package com.ruoyi.web.controller.resturant;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.resturant.domain.ResShop;
import com.ruoyi.resturant.service.IResShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 店铺Controller
 *
 * @author Daniel
 * @date 2023-09-27
 */
@Anonymous
@RestController
@RequestMapping("/api/resturant/shop")
@Api(tags="店铺管理")
public class ResShopController {
    @Autowired
    private IResShopService resShopService;


    @ApiOperation("查询店铺详情")
    @ApiImplicitParam(name = "name", value = "店铺名称", dataType = "string", dataTypeClass = String.class)
    @GetMapping("/selectResShop")
    public AjaxResult selectResShop(@RequestParam(value = "name", required = false) String shopName) {
        List<ResShop> selectResShop = resShopService.selectResShop(shopName);
        return AjaxResult.success(selectResShop);
    }

    /**
     * 获取店铺详细信息
     */
    @ApiOperation(value = "获取店铺详细信息", notes = "TODO：取得某个店铺的详细信息")
    @ApiImplicitParam(name = "id", value = "店铺ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(resShopService.selectResShopById(id));
    }


    @ApiOperation("新增店铺")
    @PostMapping("/addShop")
    public AjaxResult addResShop(@RequestBody ResShop resShop) {
        return AjaxResult.success(resShopService.addResShop(resShop));
    }


    @ApiOperation("修改店铺")
    @PutMapping("/updateResShop")
    public AjaxResult updateResShop(@RequestBody ResShop resShop) {
        return AjaxResult.success(resShopService.updateResShop(resShop));
    }

    /**
     * 删除店铺
     */
    @ApiOperation("删除店铺")
    @ApiImplicitParam(name = "id", value = "店铺ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult deleteResShopById(@PathVariable int id) {
        return AjaxResult.success(resShopService.deleteResShopById(id));
    }

    @Autowired
    IResShopService service;
    @Autowired
    RedisCache cache;

    @ApiOperation("if查询，获取店铺列表")
    @PostMapping("/selectResShopList")
    public AjaxResult selectResShopList(@RequestBody ResShop resShop){

        List<ResShop> resultList = service.selectShopList(resShop);
        for (int i=0;i< resultList.size();i++) {
            String jsonStr = JSON.toJSONString(resultList.get(i));
            Long id = resultList.get(i).getId();
            cache.setCacheObject(String.valueOf(id),jsonStr,20, TimeUnit.SECONDS);
        }

        return AjaxResult.success(resultList);
    }

    @ApiOperation("when查询，获取店铺列表")
    @PostMapping("/selectResShopList2")
    public AjaxResult selectResShopList2(@RequestBody ResShop resShop){
        return AjaxResult.success(service.selectResShopList2(resShop));
    }

    @ApiOperation("测试日志")
    @PostMapping("/selectResShopList3")
    //@Log(title = "获取店铺列表",businessType = BusinessType.OTHER)
    public AjaxResult  selectResShopList3(@RequestBody ResShop  resShop){
        return AjaxResult.success(service.selectResShopList2(resShop));
    }

    //尝试存入一个复杂类型
    // @ApiOperation(value = "根据店铺id，获取redis的数据")
    // @GetMapping("/getResShop")
    // public AjaxResult getResShop(int id){

    // }

    public void id(){
        for (int i = 10; i <= 0; i--){
            int res = 5/i;
            System.out.println(res);
        }
    }
}